//
//  GB_HotViewController.m
//  MELive
//
//  Created by 郭宝 on 16/10/23.
//  Copyright © 2016年 郭宝. All rights reserved.
//

#import "GB_HotViewController.h"
#import "GB_HotTableViewCell.h"
#import "GB_Hot.h"
#import "GB_HotData.h"
#import "GB_HotRooms.h"
#import "GB_HotUserbasicinfo.h"
#import "YYModel.h"
#import "ZWYNetTool.h"
#import "UIImageView+WebCache.h"
#import "Macro.h"
#import "BaseLabel.h"
#import "WYScrollView.h"
#import "GB_CycleViewController.h"
#import "MJRefresh.h"
#import "AFNetworking.h"

#import "GB_CycleHot.h"
#import "GB_CycleHotData.h"
#import "GB_HotExtra.h"
#import "GB_HotProperties.h"
#import "GB_HotHomeDataModel.h"
#import "GB_CycleHotDataModel.h"
#import "GB_HotHomeProperties.h"
#import "GB_UserBasicInfoViewController.h"


@interface GB_HotViewController () <UITableViewDataSource, UITableViewDelegate, WYScrollViewNetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) GB_Hot *hot;
@property (weak, nonatomic) IBOutlet UIView *tableHeaderView;
@property (nonatomic, strong) WYScrollView *cycleView;
@property (nonatomic, strong) GB_CycleHot *cycleHot;
@property (nonatomic, strong) NSMutableArray<GB_HotHomeDataModel *> *array;
@property (nonatomic, strong) GB_HotHomeDataModel *data;
@property (nonatomic, strong) GB_UserBasicInfoViewController *VC;

@end

@implementation GB_HotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self handleCycleViewData];
    _tableHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 1042 * 354);
    [_tableView registerNib:[UINib nibWithNibName:@"GB_HotTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GB_HotTableViewCell"];
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleData)];
    NSDictionary *header = @{@"token": @"6347e2e2-42d5-46d5-9e75-28d3e3d03237"};
    [ZWYNetTool GET:@"http://api1.naonaola.com/room/hot" andBody:nil andHeader:header andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _hot = [GB_Hot yy_modelWithJSON:result];
        NSLog(@"222222222222222222%@", _hot.data.rooms[0].roomCover);
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
    }];
}

- (void)handleCycleViewData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"+38MPL27POFg3ADSjiJiM6Dmvo9eGMy5,WbFDuiDblRn2Q3K1jv0n03nDjbf3fMD/qYvhbWeLihL2EUSFw7dYWwoYWGug uHw9U4rfC6rFTnvQShVpysVq6iLQwGXekDaFUA8Nc0ykF1ZjAQILAzOKPkpI KzQ/6ZuyCxsSHlnkjKd1aRelWZ2jNkpKqyCx5Nga" forHTTPHeaderField:@"X-Authorization"];
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    [manager POST:@"http://me.yy.com:80/main/getTemplateIndexData" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _cycleHot = [GB_CycleHot yy_modelWithJSON:responseObject];
        NSLog(@"%@", _hot);
        _array = [NSMutableArray array];
        _array = [NSMutableArray arrayWithArray:_cycleHot.data.data[4].data];
        _array = [NSMutableArray arrayWithArray:_cycleHot.data.data[6].data];
        NSMutableArray *array = [NSMutableArray array];
        for (GB_HotHomeDataModel *model in _cycleHot.data.data[0].data) {
            [array addObject:model.picUrl];
        }
        _cycleView = [[WYScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 1042 * 354) WithNetImages:array];
        [_cycleView setAutoScrollDelay:1];
        _cycleView.netDelagate = self;
        [_tableHeaderView addSubview:_cycleView];
        NSLog(@"111");
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
- (void)didSelectedNetImageAtIndex:(NSInteger)index {
    GB_CycleViewController *vc = [GB_CycleViewController new];
    vc.CycleTitle = _cycleHot.data.data[0].data[index].title;
    vc.linkUrl = _cycleHot.data.data[0].data[index].linkUrl;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    grayView.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:grayView];
    GB_BaseTextLabel *roomDescriptionLabel = [[GB_BaseTextLabel alloc] initWithFrame:CGRectMake(5, 25, 200, 20)];
    roomDescriptionLabel.text = _hot.data.rooms[section].roomDescription;
    [headerView addSubview:roomDescriptionLabel];
    GB_BaseTextLabel *likeCountLabel = [[GB_BaseTextLabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 105, 25, 100, 20)];
    likeCountLabel.textAlignment = 2;
    likeCountLabel.text = [NSString stringWithFormat:@"%ld 赞", (long)_hot.data.rooms[section].likeCount];
    NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld 赞", (long)_hot.data.rooms[section].likeCount]];
    [titleString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:12] range:NSMakeRange(0, likeCountLabel.text.length - 1)];
    [likeCountLabel setAttributedText:titleString];
    [headerView addSubview:likeCountLabel];
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 65;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [UIView new];
    if (section == _hot.data.rooms.count - 1) {
        UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, 5)];
        grayView.backgroundColor = [UIColor grayColor];
        [footerView addSubview:grayView];
    }
    UIImageView *avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 40, 40)];
    [footerView addSubview:avatarImageView];
    [avatarImageView sd_setImageWithURL:[NSURL URLWithString:_hot.data.rooms[section].userBasicInfo.avatar]];
    avatarImageView.layer.masksToBounds = YES;
    avatarImageView.layer.cornerRadius = 20;
    GB_BaseDetailTextLabel *roomDescriptionLabel = [[GB_BaseDetailTextLabel alloc] initWithFrame:CGRectMake(50, 20, 200, 20)];
    roomDescriptionLabel.text = _hot.data.rooms[section].userBasicInfo.nickName;
    [footerView addSubview:roomDescriptionLabel];
    GB_BaseDescriptionLabel *addressLabel = [[GB_BaseDescriptionLabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 105, 20, 100, 20)];
    addressLabel.textAlignment = 2;
    addressLabel.textColor = [UIColor lightGrayColor];
    addressLabel.text = _hot.data.rooms[section].userBasicInfo.address;
    [footerView addSubview:addressLabel];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(footerViewTapAction:)];
    [footerView addGestureRecognizer:tap];
    _VC = [[GB_UserBasicInfoViewController alloc] initWithNibName:@"GB_UserBasicInfoViewController" bundle:[NSBundle mainBundle]];
    _VC.userId = _hot.data.rooms[section].roomOwnerId;
    return footerView;
}
- (void)footerViewTapAction: (UITapGestureRecognizer  *)tap {
    [self.navigationController pushViewController:_VC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == _hot.data.rooms.count - 1) {
        return 65;
    } else {
        return 60;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _hot.data.rooms.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GB_HotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GB_HotTableViewCell" forIndexPath:indexPath];
    [cell.roomCoverImageView sd_setImageWithURL:[NSURL URLWithString:_hot.data.rooms[indexPath.section].roomCover]];
    cell.currentUserCountLabel.text = [NSString stringWithFormat:@"正在直播 %ld 在线", _hot.data.rooms[indexPath.section].currentUserCount];
    cell.currentUserCountLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
    cell.currentUserCountView.layer.masksToBounds = YES;
    cell.currentUserCountView.layer.cornerRadius = 10;
    cell.currentUserCountView.layer.borderColor = [UIColor blackColor].CGColor;
    cell.currentUserCountView.layer.borderWidth = 2;
    if (_hot.data.rooms[indexPath.section].gameName.length == 0) {
        cell.gameNameView.hidden = YES;
    } else {
    if ([_hot.data.rooms[indexPath.section].gameName isEqual: @"美播KTV"]) {
        cell.gameNameLabel.text = @"正在唱歌";

    } else {
        cell.gameNameLabel.text = @"正在玩游戏";
    }
    cell.gameNameLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
    cell.gameNameView.layer.masksToBounds = YES;
    cell.gameNameView.layer.cornerRadius = 10;
    cell.gameNameView.layer.borderColor = [UIColor blackColor].CGColor;
    cell.gameNameView.layer.borderWidth = 2;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_WIDTH;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
